﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using prjKQXS_UI.View;
using prjKQXS_DLL;
using prjKQXS_DLL.Ve_so;

namespace prjKQXS_UI.Truyen_thong
{
    public partial class frmNhapVeHuy : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapVeHuy()
        {
            InitializeComponent();
        }

       private void frmNhapVehuy_Load(object sender, EventArgs e)
        {         
           deNgayMoThuong.DateTime = DateTime.Today;
           BienTc.denngay = (DateTime)clsCommon.Parse(deNgayMoThuong.Text, 4);
           Khoitao(BienTc.denngay);           
        }

       private void Khoitao(DateTime Ngay)
       {
           Loadphochutich();
           SqlConnection conn = new SqlConnection(BienTc.connString);
           conn.Open();
           SqlDataAdapter da = new SqlDataAdapter("Select * from KETQUATT Where Convert(Datetime,Ngaymothuong,103) = Convert(Datetime,@Ngay,103)", conn);
           da.SelectCommand.Parameters.AddWithValue("@Ngay",Ngay);
           DataTable dt = new DataTable();
           da.Fill(dt);
           if (dt != null && dt.Rows.Count > 0)
           {
               teKyHieuVe.Text = dt.Rows[0]["Kyhieu"].ToString();
               cbeGiaVe.Text = dt.Rows[0]["Giave"].ToString();
           }       
       }

       private void Loadphochutich()
       {
           SqlConnection conn = new SqlConnection(BienTc.connString);
           DataTable dt = new DataTable();
           try
           {
               cbeGiaVe.Text = "10000";
               SqlDataAdapter da = new SqlDataAdapter("select Tenhoidong as TEN, * from DMHOIDONG where isPublic = 1", conn);
               da.Fill(dt);
               cmbchutich.Properties.DataSource = dt;
               cmbchutich.ItemIndex = 0;
               cmbsotaichinh.Properties.DataSource = dt;
               cmbsotaichinh.ItemIndex = 3;
               cmbthanhtra.Properties.DataSource = dt;
               cmbthanhtra.ItemIndex = 4;
               cmbcongan.Properties.DataSource = dt;
               cmbcongan.ItemIndex = 5;
               cmbsotuphap.Properties.DataSource = dt;
               cmbsotuphap.ItemIndex = 6;
               cmbmattran.Properties.DataSource = dt;
               cmbmattran.ItemIndex = 7;
               cmbcongty.Properties.DataSource = dt;
               cmbcongty.ItemIndex = 1;
               cmbthuky.Properties.DataSource = dt;
               cmbthuky.ItemIndex = 8;
           }
           catch (Exception)
           {
               throw;
           }
           finally
           {
               if (conn.State == ConnectionState.Open)
               {
                   conn.Close();
               }
           }
       }
     private void Luuhoidong()
         {
             try
             {
                 DMVETT dmvett = new DMVETT();
                 dmvett.Kyhieu = teKyHieuVe.Text;
                 dmvett.Giave = (Int32)clsCommon.Parse(cbeGiaVe.Text, 0);
                 dmvett.Ngaymothuong = (DateTime)clsCommon.Parse(deNgayMoThuong.Text, 4);
                 dmvett.Pchutich = cmbchutich.Text;
                 dmvett.Congty = cmbcongty.Text;
                 dmvett.Sotaichinh = cmbsotaichinh.Text;
                 dmvett.Sotuphap = cmbsotuphap.Text;
                 dmvett.Congantinh = cmbcongan.Text;
                 dmvett.MattranTQ = cmbmattran.Text;
                 dmvett.ThanhtranNN = cmbthanhtra.Text;
                 dmvett.Thuky = cmbthuky.Text;
                 dmcanbo_dal.InsertDMVE(dmvett);
             }
             catch
             {

             }
         }
       private DMVETT_DAL dmcanbo_dal = new DMVETT_DAL();
       private void btndong_Click(object sender, EventArgs e)
       {
           Luuhoidong();
           this.Close();
       }

       private void sbtnDaiLy_Click(object sender, EventArgs e)
       {
           int GiaVe = (Int32) clsCommon.Parse(cbeGiaVe.Text,0);
           
           if (teKyHieuVe.Text.Trim().Length > 30)
           {
               clsCommon.MsgBox("Ký hiệu vé quá dài, bạn hãy sửa lại", 0);
               teKyHieuVe.Focus();
           }
           else
           {
               frmNhapVeHuy_DL frm = new frmNhapVeHuy_DL(GiaVe, teKyHieuVe.Text.Trim(), deNgayMoThuong.DateTime);
               if (clsCommon.formIsNotExisting(frm.Name))
               {
                   //frm.MdiParent = this.ParentForm;
                   frm.WindowState = FormWindowState.Maximized;
                   //frm.ShowDialog();
                   frm.Show();
               }
               else frm.Dispose();
           }
       }

       private void txtNgay_EditValueChanged(object sender, EventArgs e)
       {
           BienTc.denngay = (DateTime)clsCommon.Parse(deNgayMoThuong.Text, 4);
           Khoitao(BienTc.denngay);           
       }

       private void btncanbo_Click(object sender, EventArgs e)
       {
           BienTc.GiaVe = (Int32)clsCommon.Parse(cbeGiaVe.Text, 0);
           if (teKyHieuVe.Text.Trim().Length > 30)
           {
               clsCommon.MsgBox("Ký hiệu vé quá dài, bạn hãy sửa lại", 0);
               teKyHieuVe.Focus();
           }
           else
           {
               frmNhapVeHuy_CB frm = new frmNhapVeHuy_CB();
               frm.GiaVe = Convert.ToInt32(cbeGiaVe.Text.Trim());
               frm.KyHieu = teKyHieuVe.Text.Trim();
               frm.WindowState = FormWindowState.Maximized;
               frm.Show();
           }
       }
       private void btncongty_Click(object sender, EventArgs e)
       {
           Luuhoidong();
           BienTc.GiaVe = (Int32)clsCommon.Parse(cbeGiaVe.Text, 0);// 19.02.2014

           if (teKyHieuVe.Text.Trim().Length > 30)
           {
               clsCommon.MsgBox("Ký hiệu vé quá dài, bạn hãy sửa lại", 0);
               teKyHieuVe.Focus();
           }
           else
           {
               frmNhapVeHuy_DV frm = new frmNhapVeHuy_DV();
               frm.GiaVe = Convert.ToInt32(cbeGiaVe.Text.Trim());
               frm.KyHieu = teKyHieuVe.Text.Trim();
               frm.WindowState = FormWindowState.Maximized;
               frm.Show();
           }
       }

       private void frmNhapVeHuy_Shown(object sender, EventArgs e)
       {
           btndaily.Enabled = clsCommon.hasPermission(BienTc.UserID, (int) BienTc.eFunction.B_btndaily);
           btncanbo.Enabled = clsCommon.hasPermission(BienTc.UserID, (int) BienTc.eFunction.B_btncanbo);
           btncongty.Enabled = clsCommon.hasPermission(BienTc.UserID, (int) BienTc.eFunction.B_btncongty);
       }

      }
}